import {createRouter, createWebHashHistory} from 'vue-router';
import PageRouter from './page/'
import ViewsRouter from './views/'
import AvueRouter from './avue-router';
import i18n from '@/lang'
import useUserStore from "@/stores/useUserStore";


let routerOption = {}
routerOption.base = import.meta.env.VITE_BASE_URL;
routerOption.history = createWebHashHistory();
routerOption.routes = [...PageRouter, ...ViewsRouter];
//创建路由
const Router = createRouter(routerOption)


/**
 * 关闭子页面
 * @param fullPath 访问路径 为空默认关闭当前页面
 * @param backNum 页面退回数量 默认：退回上一个页面
 */
Router.closeRouter = function (fullPath, backNum = -1) {
    Router.$avueRouter.closeTag(fullPath)
    Router.go(backNum)
}
Router.close = function (fullPath, backNum = -1) {
    Router.closeRouter(fullPath, backNum)
}
const srcRouterInstall = Router.install

Router.install = function (app, {pinia}) {
    AvueRouter.install({
        pinia: pinia,
        router: Router,
        i18n: i18n
    });
    const userStore = useUserStore()

    Router.$avueRouter.formatRoutes(userStore.menuAll, true);
    Router.install = srcRouterInstall
    Router.install(app)
}

export default Router
